<?php
/*
 * @Author: your name
 * @Date: 2020-10-14 10:23:43
 * @LastEditTime: 2020-12-31 17:05:43
 * @LastEditors: Please set LastEditors
 * @Description: 访问日志验证器
 * @FilePath: /special/application/adminapiv3/validate/AccessLogValidate.php
 */

namespace app\adminapiv3\validate;

use think\Validate;

class AccessLogValidate extends Validate
{
    protected $rule = [
        //来源地址
        'source_ip|来源IP' => 'require|ip',
        //目标地址
        'target_ip|目标IP' => 'require|ip',
        //是否忽略 0不忽略 1忽略
        'ignore|是否忽略' => 'require|in:0,1',
        //流量方向 1出站 2入站
        'direction|流量方向' => 'in:1,2',
        //目标端口号
        'target_port|目标端口' => 'number|between:1,65535',
        //协议  6 TCP 17 UDP
        'protocols|网络协议' => 'in:6,17',
        //是否授权
        'permission|是否授权' => 'in:0,1',
        //第一次记录时间
        'first_time|第一次记录时间' => 'date',
        //最后更新时间
        'last_time|最后更新时间' => 'date',
        //主机id
        'host_id|主机ID' => 'require|regex:^\d{1,8}(,\d{1,8}){0,30}$',
        //日志最大过期时间 (s)
        'expired_time|日志最大过期时间' => 'require|number|length:1,10',
        //日志收集频率 单位 (秒)
        'frequency|日志手机频率' => 'require|number|length:1,10',
        //日志超期处理方式 1删除 2继续记录
        'process|日志超期处理方式' => 'require|in:1,2',
        //日志id
        'id|日志ID' => 'require|regex:^\d{1,8}(,\d{1,8}){0,30}$',
    ];

    //错误消息
    protected $message = [
        'ignore.in' => '是否忽略格式错误',
        'direction.in' => '流量方向格式错误',
        'protocols.in' => '网络协议格式错误',
        'permission.in' => '是否授权格式错误',
        'process.in' => '日志超期处理方式格式错误',
    ];

    protected $scene = [
        //ip组统计列表筛选
        // 'ipGroupListFilter' => ['source_ip','target_ip','ignore'],
        //ip组详细列表筛选
        'ipGroupDetailFilter' => ['source_ip','direction','target_ip','target_port','protocols','permission','first_time','last_time','ignore'],
        //修改日志设置
        'modifyConfig' => ['host_id','expired_time','frequency','process'],
        //忽略日志
        'ignoreLog' => ['id'],
        //告警列表
        // 'warringListFilter' => ['target_ip','source_ip','direction','protocols','permission','target_port'],
    ];

    //ip组统计列表筛选
    public function sceneIpGroupListFilter()
    {
        return $this->only(['source_ip','target_ip','ignore'])->remove('source_ip', 'require')->remove('target_ip', 'require');
    }
    
    //告警列表
    public function sceneWarringListFilter()
    {
        return $this->only(['target_ip','source_ip','direction','protocols','permission','target_port'])->remove('source_ip', 'require')->remove('target_ip', 'require');
    }
}
